package com.mpower.org;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author mpower
 */
public class MyFirstServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ServletConfig config = getServletConfig();
        ServletContext context = config.getServletContext();
        String firstName=request.getParameter("fname");
        String lastName=request.getParameter("lname");
        String fullName;
        if(firstName==null) {
            firstName="";
        }
        if(lastName==null) {
            lastName="";
        }
        fullName=firstName+" "+lastName;
        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>MyFirstServlet</title>");
            out.println("</head>");
            out.println("<body>");
           // Cookie cookie=new Cookie("fullName", fullName);
            //cookie.setMaxAge(300);
            //response.addCookie(cookie);
            HttpSession session=request.getSession();
            session.setAttribute("fullName", fullName);
            out.println("<p align=\"right\">Welcome " + fullName + "</p>");
            out.println("First Name:" + firstName + "<br/>");
            out.println("Last Name:" + lastName + "<br/>");
            out.println("Email:" + request.getParameter("email") + "<br/>");
            out.println("Games:");
            int age = Integer.parseInt(request.getParameter("age").trim());
            String category = "";
            String[] games = request.getParameterValues("games");
            if (games != null) {
                for (String game : games) {
                    out.println(game + "&nbsp;&nbsp;&nbsp;&nbsp;");
                }
            }
            if (age < 10) {
                category = "Out of games";
            } else if (age >= 10 && age <= 13) {
                category = "Sub Junior";
            } else if (age >= 14 && age <= 15) {
                category = "Junior";
            } else if (age > 15 && age < 20) {
                category = "Senior";
            } else {
                  RequestDispatcher rd = request.getRequestDispatcher("MySecondServlet");
                rd.forward(request, response);//forwards to /second which is MySecondServlet
                rd.include(request, response);
                //response.sendRedirect("http://www.google.com");
            }
            out.println("Category:" + category);
            out.println("<br/>");
            out.println("Copyright reserved. Author:" + config.getInitParameter("author"));
            out.println("<a href=\"http://localhost:8083/Servlets_2/MySecondServlet\">Second Page</a>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
